Skip to content

Conversation

JoshuaEngland
Copy link

FreePort was quite inflexible in how it picked ports. If multiple
threads, or processes, were concurrently trying to look for free ports
in a given range there is a high likelihood of collision where they both
think a port is free then race to bind to it.

This change adds strategies for how to pick the ports.

The LinearFreePortStrategy reflects the old strategy of linearly
walking the port range and trying each port.

The RandomizedFreePortStrategy is a new strategy of repeatedly trying
ports in a given range in order to avoid collisions.

This doesn't remove the race condition as theoretically the same
situation can occur with the RandomizedFreePortStrategy as the free port
is never secured. However, it does mitigate against it and reduce the
likelihood of collisions over a big enough range.

FreePort was quite inflexible in how it picked ports. If multiple
threads, or processes, were concurrently trying to look for free ports
in a given range there is a high likelihood of collision where they both
think a port is free then race to bind to it.

This change adds strategies for how to pick the ports.

The LinearFreePortStrategy reflects the old strategy of linearly
walking the port range and trying each port.

The RandomizedFreePortStrategy is a new strategy of repeatedly trying
ports in a given range in order to avoid collisions.

This doesn't remove the race condition as theoretically the same
situation can occur with the RandomizedFreePortStrategy as the free port
is never secured. However, it does mitigate against it and reduce the
likelihood of collisions over a big enough range.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant